package com.cloudshadow.platform.server.common.base.utils;


import com.cloudshadow.platform.infrastructure.exception.ErrorInfoBase;
import com.cloudshadow.platform.server.common.base.vo.ErrorBaseVo;
import com.cloudshadow.platform.server.common.base.vo.BasicVo;

public abstract class VoUtil {

    public static <T extends BasicVo> T genErrorResult(Integer errorNo, String errorInfo, Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            return setErrorResult(errorNo, errorInfo, t);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T extends BasicVo> T genErrorResult(ErrorInfoBase errInfo, Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            return setErrorResult(errInfo, t);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static BasicVo genErrorResult(ErrorInfoBase errInfo) {
        return setErrorResult(errInfo.getError_no(),errInfo.getError_info(), new BasicVo());
    }

    public static <T extends BasicVo> T setErrorResult(ErrorInfoBase errInfo, T result) {
        result.setError(new ErrorBaseVo(errInfo.getError_no(),errInfo.getError_info()));
        return result;
    }

    public static <T> BasicVo getSuccessResult() {
        return new BasicVo(true,null,null);
    }

    public static <T> BasicVo setSuccessResult(T result) {
        return new BasicVo(true,result,null);
    }

    public static <T> BasicVo setSuccessResult() {
        return new BasicVo(true,null,null);
    }
    
    public static <T extends BasicVo> T setErrorResult(Integer errorNo, String errorInfo, T result) {
        result.setError(new ErrorBaseVo(errorNo,errorInfo));
        return result;
    }

    public static boolean isSuccess(BasicVo res) {
        return res != null && res.isSuccess();
    }
}
